/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.general;

import im.node.MobileNodeInfo;
import im.resources.ResourceStatus;
import im.subscriptionpackage.ContentSubscription;
import jade.core.ContainerID;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author bluejack
 */
public class BaseSuperAgent extends BaseAgent {
    protected Hashtable<String,MobileNodeInfo> nodes;

    public BaseSuperAgent() {
        nodes = new Hashtable();
    }

    @Override
    public void setup() {
        super.setup();        
    }
     
    public void addNode(String name, MobileNodeInfo info) {
        nodes.put(name, info);
    }
    public void removeNode(String name) {
        nodes.remove(name);
    }
    public MobileNodeInfo getNodeInfo(String name) {

        try {
        if( !name.contains("@") )
            throw new UnsupportedOperationException("BAD node name format.");

        if(nodes.containsKey(name))
            return (MobileNodeInfo) nodes.get(name);

        } catch (Exception ex) {
            System.out.println(this.getClass().getCanonicalName() + "$" + this.getLocalName() + "| " + name + "? -> " + nodes.toString());
            Logger.getLogger(MobileNodeInfo.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(" PROBLEMA:\ntutti i nomi relativi ad i nodi mobili DEVONO essere usati nel formato GLOBAL e non LOCAL\n\nESEMPIO:\n\nNO: 5\nSI  : 5@GNASKYPAD:1099/JADE\n\nQuesto serve perchè le Hashtables sono indicizzate per GLOBAL_name e NON per local_name.\nPer facilitare la conversione ho fatto in modo che il sistema lanci questa ECCEZIONE quando cio' non accade.\nSe non si fa questa modifica non funziona un mazza, nemmeno con Sofia Gucci :).\n -> Daniele :)");
            System.out.flush();
            System.out.println( ex.toString() );
            System.out.flush();
            //ex.printStackTrace();
        }

        return null;
    }

    public void setContentSubscription(String node, ContentSubscription subscription) {
        MobileNodeInfo info = getNodeInfo(node);
        if(info==null) {
            //Note: CHANGED because nodeinfo reporting mechanism NEEDS global names.
            //old:
            //info = new MobileNodeInfo(node, this.getLocalName(), null, null);
            //TODO: check null parameters
            // by Daniele
            //info = new MobileNodeInfo(node, this.getName(), null, null);
            //setNodeInfo(node, info);
            return;
        }
        
        info.setContentSubscription(subscription);
    }
    
    public void setNodeInfo(String name, MobileNodeInfo info) {
        nodes.put(name, info);
    }

    public void setResourceStatus(String node, ResourceStatus resourceStatus) {
        MobileNodeInfo info = getNodeInfo(node);
        if(info==null) {
            //info = new MobileNodeInfo(node, this.getName(), resourceStatus, null);
            return;
        }
        else
            info.setResourceStatus(resourceStatus);
        //setNodeInfo(node, info);
    }
    
    public Enumeration getNodeInfos() {
        return nodes.elements(); //nodes.
    }

    public Enumeration getNodeNames() {
        return nodes.keys(); //nodes.
    }
    
    public Iterator getInfoIterator() {
        return nodes.values().iterator();
    }
    
    public void moveAgent(String container) {
        if(container == null)
            return;
        
       ContainerID c = new ContainerID();
       c.setName(container);
       
       this.container = container;
       this.doMove(c);
    }

    @Override
    public int getAttachedNodes() {
        return nodes.size();
    }

    @Override
    public void setAttachedNodes(int attachedNodes) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
